/**
 * 
 */
package com.metacube.questionbank.utility;

/**
 * @author Admin39
 *
 */

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import com.metacube.questionbank.dao.UserDAO;
import com.metacube.questionbank.model.User;


/*
 * @author Ankit
 */
public class Validator {
	private static final Logger logger = LoggerFactory.getLogger(Validator.class);

	static Pattern pattern;
	static Matcher matcher;
	public static boolean comparepasswords(String password,String confirmpassword)
	{
		if(password.equals(confirmpassword))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public static int checkImage(MultipartFile image)
	{
		if(image.getSize()==0)
		{
			return -1;	
		}
		else
		{
			if(image.getContentType().contains("image"))
			{ 
				if(image.getSize()>4000)
				{
					logger.info("size > than 4000");

					return 1;
				}
				else
				{
					logger.info("size < than 4000");

					return 0;
				}
			}
			else
			{
				System.out.println("Wrong extension");
				return 2;
			}
		}
	}

	/**
	 * this function validate the email
	 * @param Email
	 * @return boolean
	 */
	public static boolean Email(String Email){
		String emailpattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

		Pattern pattern1 = Pattern.compile(emailpattern);

		Matcher matcher1= pattern1.matcher(Email);

		if(matcher1.matches()){

			return true;
		}
		else{

			return false;
		}

	}
	/*
	 * checking if password is of valid syntax
	 * @method checkPassword
	 * @param  password
	 */
	private static  boolean checkPasswordpatten(String password)
	{
		final String passwordpattern="(?=^.{8,}$)((?=.* \\d)|(?=.*\\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$";

		if(!password.matches(passwordpattern))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/*
	 * checking if user information is not empty
	 * @method checkEmpty
	 * @param  userData
	 */
	public static  boolean checkEmpty(String userData)
	{
		boolean flag;
		if(userData==null || userData.isEmpty())
		{
			flag=false;
		}
		else
		{
			flag=true;
		}
		return flag;
	}
	public static java.util.Date getDate(String date)
	{
		
		SimpleDateFormat dateFormat=new SimpleDateFormat("dd-mm-yyyy");
		try {
			return 	dateFormat.parse(date);	
				
			} catch (ParseException e) {
					return null;
			}		
		
	}
	public static boolean validDateSyntax(String date)
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat("dd-mm-yyyy");
			try {
					dateFormat.parse(date);	
					return true;
				} catch (ParseException e) {
						return false;
				}		
	}
	public static boolean validateDate(Date date)
	{
		java.util.Date validatedate = date;

		SimpleDateFormat dateFormat=new SimpleDateFormat("dd-mm-yyyy");
		
		try {
			java.util.Date afterdate=dateFormat.parse("11-11-1914");
			java.util.Date beforedate=dateFormat.parse("11-11-2014");

			java.sql.Date sqldate = new java.sql.Date(validatedate.getTime());
			if(sqldate.after((afterdate)) && sqldate.before(beforedate))
			{
				return true;
			}
			else
			{
				return false;
			}
		} catch (ParseException e) {
			return false;
		}
	}
	/*
	 * Checking if data entered by user is valid
	 * @method validData
	 * @param Users object
	 */
	public static boolean validateProfileEdit(User user)

	{
		if(Validator.checkEmpty(user.getGender()) && Validator.checkEmpty(user.getAbout_me()) && Validator.checkEmpty(user.getDob().toString())   )
		{
			return true;
		}
		else
		{
			return false;
		}
	}



}
